"""
字典推导式

格式：
    {key_expr: value_expr for value in collection if condition}

"""

# 例1：用字典推导式以字符串以及其长度建字典

strings = ["import", "is", "with", "if", "file", "exception"]
D = {key: val for val, key in enumerate(strings)}
print(D)

# 例2：大小写key合并
# 从字典中取值dict.get("key", default_value)，如果没有取到key，返回default_value

mcase = {"a": 10, "b": 34, "A": 7, "Z": 3}

mcase_frequency = {
    k.lower(): mcase.get(k.lower(), 0) + mcase.get(k.upper(), 0)
    for k in mcase.keys()
    if k.lower() in ['a', 'b']
}

print(mcase_frequency)

# 例3：字典的key,value互换
D = {"aa":"aa1","bb":"bb1","cc":"cc1"}
result = {D[key]: key for key in D.keys()}
print(result)


